<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      ul,
      ol {
        list-style: none;
      }
      .carousel {
        position: relative;
        width: 880px;
        height: 550px;
        border: 1px solid #333;
        margin: 50px auto;
      }
      .pic li {
        position: absolute;
        left: 0;
        top: 0;
        width: 880px;
        height: 550px;
        display: none;
      }
      .pic li.current {
        display: block;
      }
      .btn a {
        position: absolute;
        top: 50%;
        width: 80px;
        height: 80px;
        margin-top: -40px;
        background-color: rgba(255, 255, 255, 0.3);
        text-decoration: none;
        color: #444;
        text-align: center;
        line-height: 80px;
        font-size: 60px;
        font-family: 'SimSun';
      }
      .btn .left {
        left: 10px;
      }
      .btn .right {
        right: 10px;
      }
      .btn a:hover {
        background-color: rgba(255, 255, 255, 0.7);
      }
      .sub {
        position: absolute;
        bottom: 30px;
        left: 50%;
        width: 200px;
        height: 40px;
        margin-left: -100px;
        border-radius: 20px;
        background-color: rgba(255, 255, 255, 0.3);
      }
      .sub li {
        float: left;
        width: 20px;
        height: 20px;
        margin: 10px;
        border-radius: 50%;
        background-color: #ccc;
        cursor: pointer;
      }
      .sub li.current {
        background-color: #0ff;
      }
    </style>
  </head>
  <body>
    <div class="carousel" id="carousel">
      <ul class="pic" id="pic">
        <li class="current"><img src="image/lunbo/01.jpg" alt="" /></li>
        <li><img src="image/lunbo/02.jpg" alt="" /></li>
        <li><img src="image/lunbo/03.jpg" alt="" /></li>
        <li><img src="image/lunbo/04.jpg" alt="" /></li>
        <li><img src="image/lunbo/05.jpg" alt="" /></li>
      </ul>
      <div class="btn" id="btn">
        <a href="javascript:;" class="left" id="leftbtn">&lt;</a>
        <a href="javascript:;" class="right" id="rightbtn">&gt;</a>
      </div>
      <ol class="sub" id="sub">
        <li class="current"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ol>
    </div>
    <script>
      // 1.获取元素
      var carousel = my$('carousel') //容器
      var ul = my$('pic') //ul
      var ulLis = ul.children //所有轮播图的li标签
      var leftbtn = my$('leftbtn') //左边切换按钮
      var rightbtn = my$('rightbtn') //右边切换按钮
      var ol = my$('sub') //轮播图角标容器
      var olLis = ol.children //轮播图角标
      //2.全局变量
      var index = 0 //存储展示的图片所在的索引
      var timer //定时器
      //3.右边按钮点击事件
      rightbtn.onclick = rightHandle
      //4.左边按钮点击事件
      leftbtn.onclick = leftHandle
      //5.点击对应ol切换轮播图
      ol.onclick = function (event) {
        if (event.target !== this) {
          //找到当前元素下角标赋值给全局索引
          index = [...olLis].indexOf(event.target)
          change()
        }
      }
      //6.自动轮播，自动轮播相当于向右切换事件
      autoPlay()
      //7.鼠标移动到轮播图自动轮播停止，离开轮播图后自动播放
      carousel.onmouseenter = clearTimer
      carousel.onmouseleave = autoPlay
      //向左切换处理事件
      function leftHandle() {
        index--
        if (index < 0) {
          index = ulLis.length - 1
        }
        change()
      }
      //向右切换处理事件
      function rightHandle() {
        index++
        if (index >= ulLis.length) {
          index = 0
        }
        change()
      }
      //切换事件
      function change() {
        //清除其他
        for (var i = 0; i < ulLis.length; i++) {
          ulLis[i].className = ''
          olLis[i].className = ''
        }
        //保留当前
        ulLis[index].className = 'current'
        olLis[index].className = 'current'
      }
      //自动轮播
      function autoPlay() {
        timer = setInterval(rightHandle, 2000)
      }
      //停止自动轮播
      function clearTimer() {
        clearInterval(timer)
      }
      //通过id获取元素
      function my$(id) {
        return document.getElementById(id)
      }
    </script>
  </body>
</html>
